using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MesProjectWinform.用户控件
{
    /// <summary>
    /// 分页控件
    /// </summary>
    public partial class PaginationControl : XtraUserControl
    {
        #region 属性和事件

        private int _totalRecords = 0;
        private int _currentPage = 1;
        private int _pageSize = 10;
        private List<int> _pageSizeOptions = new List<int> { 5, 10, 20, 50, 100 };

        /// <summary>
        /// 总记录数
        /// </summary>
        [Browsable(true), Category("分页"), Description("总记录数")]
        public int TotalRecords
        {
            get { return _totalRecords; }
            set
            {
                _totalRecords = value;
                UpdatePageInfo();
            }
        }

        /// <summary>
        /// 当前页码
        /// </summary>
        [Browsable(true), Category("分页"), Description("当前页码")]
        public int CurrentPage
        {
            get { return _currentPage; }
            set
            {
                if (value < 1)
                    _currentPage = 1;
                else if (value > TotalPages && TotalPages > 0)
                    _currentPage = TotalPages;
                else
                    _currentPage = value;

                UpdatePageInfo();
            }
        }

        /// <summary>
        /// 每页记录数
        /// </summary>
        [Browsable(true), Category("分页"), Description("每页记录数")]
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                _pageSize = value;
                UpdatePageInfo();
            }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        [Browsable(false)]
        public int TotalPages
        {
            get
            {
                if (_pageSize <= 0) return 0;
                return (int)Math.Ceiling((double)_totalRecords / _pageSize);
            }
        }

        /// <summary>
        /// 每页记录数选项
        /// </summary>
        [Browsable(true), Category("分页"), Description("每页记录数选项")]
        public List<int> PageSizeOptions
        {
            get { return _pageSizeOptions; }
            set
            {
                _pageSizeOptions = value;
                InitPageSizeDropDown();
            }
        }

        /// <summary>
        /// 页面改变事件
        /// </summary>
        [Browsable(true), Category("分页"), Description("页面改变事件")]
        public event EventHandler<PageChangedEventArgs> PageChanged;

        /// <summary>
        /// 每页记录数改变事件
        /// </summary>
        [Browsable(true), Category("分页"), Description("每页记录数改变事件")]
        public event EventHandler<PageSizeChangedEventArgs> PageSizeChanged;

        #endregion

        #region 控件初始化

        public PaginationControl()
        {
            InitializeComponent();
            InitPageSizeDropDown();
            UpdatePageInfo();
        }

        private void InitializeComponent()
        {
            this.lblTotal = new DevExpress.XtraEditors.LabelControl();
            this.btnFirst = new DevExpress.XtraEditors.SimpleButton();
            this.btnPrev = new DevExpress.XtraEditors.SimpleButton();
            this.txtCurrentPage = new DevExpress.XtraEditors.TextEdit();
            this.btnNext = new DevExpress.XtraEditors.SimpleButton();
            this.btnLast = new DevExpress.XtraEditors.SimpleButton();
            this.lblPageCount = new DevExpress.XtraEditors.LabelControl();
            this.btnGo = new DevExpress.XtraEditors.SimpleButton();
            this.lblPageSize = new DevExpress.XtraEditors.LabelControl();
            this.cmbPageSize = new DevExpress.XtraEditors.ComboBoxEdit();
            this.txtJumpTo = new DevExpress.XtraEditors.TextEdit();
            this.lblJumpTo = new DevExpress.XtraEditors.LabelControl();
            ((System.ComponentModel.ISupportInitialize)(this.txtCurrentPage.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.cmbPageSize.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtJumpTo.Properties)).BeginInit();
            this.SuspendLayout();
            // 
            // lblTotal
            // 
            this.lblTotal.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.lblTotal.Appearance.Options.UseFont = true;
            this.lblTotal.Location = new System.Drawing.Point(10, 13);
            this.lblTotal.Name = "lblTotal";
            this.lblTotal.Size = new System.Drawing.Size(92, 18);
            this.lblTotal.TabIndex = 0;
            this.lblTotal.Text = "共 0 条数据";
            // 
            // btnFirst
            // 
            this.btnFirst.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.btnFirst.Appearance.Options.UseFont = true;
            this.btnFirst.Location = new System.Drawing.Point(158, 10);
            this.btnFirst.Name = "btnFirst";
            this.btnFirst.Size = new System.Drawing.Size(25, 25);
            this.btnFirst.TabIndex = 1;
            this.btnFirst.Text = "<<";
            this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
            // 
            // btnPrev
            // 
            this.btnPrev.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.btnPrev.Appearance.Options.UseFont = true;
            this.btnPrev.Location = new System.Drawing.Point(189, 10);
            this.btnPrev.Name = "btnPrev";
            this.btnPrev.Size = new System.Drawing.Size(25, 25);
            this.btnPrev.TabIndex = 2;
            this.btnPrev.Text = "<";
            this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);
            // 
            // txtCurrentPage
            // 
            this.txtCurrentPage.EditValue = "1";
            this.txtCurrentPage.Location = new System.Drawing.Point(220, 10);
            this.txtCurrentPage.Name = "txtCurrentPage";
            this.txtCurrentPage.Properties.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.txtCurrentPage.Properties.Appearance.Options.UseFont = true;
            this.txtCurrentPage.Properties.Appearance.Options.UseTextOptions = true;
            this.txtCurrentPage.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            this.txtCurrentPage.Properties.Mask.EditMask = "d";
            this.txtCurrentPage.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            this.txtCurrentPage.Size = new System.Drawing.Size(40, 24);
            this.txtCurrentPage.TabIndex = 3;
            this.txtCurrentPage.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtCurrentPage_KeyDown);
            // 
            // btnNext
            // 
            this.btnNext.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.btnNext.Appearance.Options.UseFont = true;
            this.btnNext.Location = new System.Drawing.Point(266, 10);
            this.btnNext.Name = "btnNext";
            this.btnNext.Size = new System.Drawing.Size(25, 25);
            this.btnNext.TabIndex = 4;
            this.btnNext.Text = ">";
            this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
            // 
            // btnLast
            // 
            this.btnLast.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.btnLast.Appearance.Options.UseFont = true;
            this.btnLast.Location = new System.Drawing.Point(297, 10);
            this.btnLast.Name = "btnLast";
            this.btnLast.Size = new System.Drawing.Size(25, 25);
            this.btnLast.TabIndex = 5;
            this.btnLast.Text = ">>";
            this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
            // 
            // lblPageCount
            // 
            this.lblPageCount.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.lblPageCount.Appearance.Options.UseFont = true;
            this.lblPageCount.Location = new System.Drawing.Point(328, 13);
            this.lblPageCount.Name = "lblPageCount";
            this.lblPageCount.Size = new System.Drawing.Size(45, 18);
            this.lblPageCount.TabIndex = 6;
            this.lblPageCount.Text = "/ 0 页";
            // 
            // btnGo
            // 
            this.btnGo.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.btnGo.Appearance.Options.UseFont = true;
            this.btnGo.Location = new System.Drawing.Point(534, 10);
            this.btnGo.Name = "btnGo";
            this.btnGo.Size = new System.Drawing.Size(40, 25);
            this.btnGo.TabIndex = 10;
            this.btnGo.Text = "跳转";
            this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
            // 
            // lblPageSize
            // 
            this.lblPageSize.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.lblPageSize.Appearance.Options.UseFont = true;
            this.lblPageSize.Location = new System.Drawing.Point(389, 13);
            this.lblPageSize.Name = "lblPageSize";
            this.lblPageSize.Size = new System.Drawing.Size(36, 18);
            this.lblPageSize.TabIndex = 7;
            this.lblPageSize.Text = "每页:";
            // 
            // cmbPageSize
            // 
            this.cmbPageSize.Location = new System.Drawing.Point(430, 10);
            this.cmbPageSize.Name = "cmbPageSize";
            this.cmbPageSize.Properties.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.cmbPageSize.Properties.Appearance.Options.UseFont = true;
            this.cmbPageSize.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.cmbPageSize.Size = new System.Drawing.Size(70, 24);
            this.cmbPageSize.TabIndex = 8;
            this.cmbPageSize.SelectedIndexChanged += new System.EventHandler(this.cmbPageSize_SelectedIndexChanged);
            // 
            // txtJumpTo
            // 
            this.txtJumpTo.EditValue = "1";
            this.txtJumpTo.Location = new System.Drawing.Point(534, 10);
            this.txtJumpTo.Name = "txtJumpTo";
            this.txtJumpTo.Properties.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.txtJumpTo.Properties.Appearance.Options.UseFont = true;
            this.txtJumpTo.Properties.Appearance.Options.UseTextOptions = true;
            this.txtJumpTo.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            this.txtJumpTo.Properties.Mask.EditMask = "d";
            this.txtJumpTo.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            this.txtJumpTo.Size = new System.Drawing.Size(40, 24);
            this.txtJumpTo.TabIndex = 9;
            this.txtJumpTo.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtJumpTo_KeyDown);
            // 
            // lblJumpTo
            // 
            this.lblJumpTo.Appearance.Font = new System.Drawing.Font("Tahoma", 9F);
            this.lblJumpTo.Appearance.Options.UseFont = true;
            this.lblJumpTo.Location = new System.Drawing.Point(505, 13);
            this.lblJumpTo.Name = "lblJumpTo";
            this.lblJumpTo.Size = new System.Drawing.Size(24, 18);
            this.lblJumpTo.TabIndex = 11;
            this.lblJumpTo.Text = "跳至";
            // 
            // PaginationControl
            // 
            this.Appearance.BackColor = System.Drawing.Color.White;
            this.Appearance.Options.UseBackColor = true;
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 18F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.lblJumpTo);
            this.Controls.Add(this.txtJumpTo);
            this.Controls.Add(this.btnGo);
            this.Controls.Add(this.cmbPageSize);
            this.Controls.Add(this.lblPageSize);
            this.Controls.Add(this.lblPageCount);
            this.Controls.Add(this.btnLast);
            this.Controls.Add(this.btnNext);
            this.Controls.Add(this.txtCurrentPage);
            this.Controls.Add(this.btnPrev);
            this.Controls.Add(this.btnFirst);
            this.Controls.Add(this.lblTotal);
            this.Name = "PaginationControl";
            this.Size = new System.Drawing.Size(600, 45);
            ((System.ComponentModel.ISupportInitialize)(this.txtCurrentPage.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.cmbPageSize.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.txtJumpTo.Properties)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private DevExpress.XtraEditors.LabelControl lblTotal;
        private DevExpress.XtraEditors.SimpleButton btnFirst;
        private DevExpress.XtraEditors.SimpleButton btnPrev;
        private DevExpress.XtraEditors.TextEdit txtCurrentPage;
        private DevExpress.XtraEditors.SimpleButton btnNext;
        private DevExpress.XtraEditors.SimpleButton btnLast;
        private DevExpress.XtraEditors.LabelControl lblPageCount;
        private DevExpress.XtraEditors.SimpleButton btnGo;
        private DevExpress.XtraEditors.LabelControl lblPageSize;
        private DevExpress.XtraEditors.ComboBoxEdit cmbPageSize;
        private DevExpress.XtraEditors.TextEdit txtJumpTo;
        private DevExpress.XtraEditors.LabelControl lblJumpTo;

        #endregion

        #region 事件处理方法

        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (CurrentPage != 1)
            {
                CurrentPage = 1;
                OnPageChanged();
            }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (CurrentPage > 1)
            {
                CurrentPage--;
                OnPageChanged();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (CurrentPage < TotalPages)
            {
                CurrentPage++;
                OnPageChanged();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (CurrentPage != TotalPages && TotalPages > 0)
            {
                CurrentPage = TotalPages;
                OnPageChanged();
            }
        }

        private void txtCurrentPage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    int page = Convert.ToInt32(txtCurrentPage.Text);
                    if (page != CurrentPage)
                    {
                        CurrentPage = page;
                        OnPageChanged();
                    }
                }
                catch
                {
                    txtCurrentPage.Text = CurrentPage.ToString();
                }
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                int page = Convert.ToInt32(txtJumpTo.Text);
                if (page != CurrentPage)
                {
                    CurrentPage = page;
                    OnPageChanged();
                }
            }
            catch
            {
                txtJumpTo.Text = CurrentPage.ToString();
            }
        }

        private void txtJumpTo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnGo_Click(sender, e);
            }
        }

        private void cmbPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int newPageSize = Convert.ToInt32(cmbPageSize.SelectedItem);
                if (newPageSize != PageSize)
                {
                    int oldPageSize = PageSize;
                    PageSize = newPageSize;
                    
                    // 计算新的当前页，保持数据在视图中的位置不变
                    int firstItemIndex = (CurrentPage - 1) * oldPageSize + 1;
                    CurrentPage = (firstItemIndex - 1) / newPageSize + 1;
                    
                    OnPageSizeChanged();
                }
            }
            catch
            {
                cmbPageSize.SelectedItem = PageSize.ToString();
            }
        }

        #endregion

        #region 辅助方法

        /// <summary>
        /// 初始化每页记录数下拉框
        /// </summary>
        private void InitPageSizeDropDown()
        {
            cmbPageSize.Properties.Items.Clear();
            foreach (int size in PageSizeOptions)
            {
                cmbPageSize.Properties.Items.Add(size.ToString());
                if (size == PageSize)
                {
                    cmbPageSize.SelectedItem = size.ToString();
                }
            }

            // 如果没有设置选中项，则默认选择第一项
            if (cmbPageSize.SelectedIndex == -1 && cmbPageSize.Properties.Items.Count > 0)
            {
                cmbPageSize.SelectedIndex = 0;
                PageSize = Convert.ToInt32(cmbPageSize.SelectedItem);
            }
        }

        /// <summary>
        /// 更新分页信息显示
        /// </summary>
        private void UpdatePageInfo()
        {
            // 更新总记录数显示
            lblTotal.Text = $"共 {TotalRecords} 条数据";

            // 更新总页数显示
            lblPageCount.Text = $"/ {TotalPages} 页";

            // 更新当前页输入框
            txtCurrentPage.Text = CurrentPage.ToString();
            txtJumpTo.Text = CurrentPage.ToString();

            // 更新按钮状态
            btnFirst.Enabled = btnPrev.Enabled = (CurrentPage > 1);
            btnNext.Enabled = btnLast.Enabled = (CurrentPage < TotalPages);
        }

        /// <summary>
        /// 触发页面改变事件
        /// </summary>
        protected virtual void OnPageChanged()
        {
            PageChanged?.Invoke(this, new PageChangedEventArgs(CurrentPage, PageSize));
        }

        /// <summary>
        /// 触发每页记录数改变事件
        /// </summary>
        protected virtual void OnPageSizeChanged()
        {
            PageSizeChanged?.Invoke(this, new PageSizeChangedEventArgs(CurrentPage, PageSize));
            OnPageChanged();
        }

        #endregion
    }

    /// <summary>
    /// 页面改变事件参数
    /// </summary>
    public class PageChangedEventArgs : EventArgs
    {
        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage { get; private set; }

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize { get; private set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页记录数</param>
        public PageChangedEventArgs(int currentPage, int pageSize)
        {
            CurrentPage = currentPage;
            PageSize = pageSize;
        }
    }

    /// <summary>
    /// 每页记录数改变事件参数
    /// </summary>
    public class PageSizeChangedEventArgs : EventArgs
    {
        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage { get; private set; }

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize { get; private set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页记录数</param>
        public PageSizeChangedEventArgs(int currentPage, int pageSize)
        {
            CurrentPage = currentPage;
            PageSize = pageSize;
        }
    }
} 